<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="名称" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="编号" prop="code">
        <el-input v-model="queryParams.code" placeholder="编号" clearable @keyup.enter.native="handleQuery()" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['goods:directory:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['goods:directory:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['goods:directory:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['goods:directory:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table @selection-change="handleSelectionChange" fit v-loading="loading" border :data="dataList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column min-width="150" label="名称" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-link @click="handleDetail(scope.row)" type="primary">{{
            scope.row.name
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="编号" min-width="150" prop="code" />
      <el-table-column :show-overflow-tooltip="true" min-width="120" label="类别" prop="categoryLabel">
      </el-table-column>
      <!-- <el-table-column
        show-overflow-tooltip
        label="技术指标"
        min-width="150"
        prop="tecTarget"
      /> -->
      <el-table-column show-overflow-tooltip min-width="150" label="技术要求" prop="techInfo" />

      <el-table-column show-overflow-tooltip min-width="150" label="主要用途" prop="useInfo" />
      <el-table-column show-overflow-tooltip label="规格" width="150" prop="spec" />

      <el-table-column show-overflow-tooltip min-width="150" label="管控等级" prop="levelLabel" />

      <el-table-column label="是否启用" v-if="$auth.hasPermi('goods:directory:status:change')" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.effectStatus" active-value="1" inactive-value="0"
            @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" v-hasPermi="['goods:directory:info']"
            @click="handleDetail(scope.row)">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['goods:directory:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['goods:directory:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import {
  listgoodsDirectory,
  delgoodsDirectory,
  effectgoodsDirectory,
} from "@/api/quality/goods/directory.js";
export default {
  name: "directory",
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      dataList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        chemicals: 0,
        name: undefined,
      },
      // 日期范围
      dateRange: [],

      userList: [],
      deptOptions: [],
    };
  },
  created() {
    const str = sessionStorage.getItem('queryParams');
    if (str) {
      this.queryParams = JSON.parse(str);
      setTimeout(() => {
        sessionStorage.removeItem('queryParams');
      }, 500)
    }
    this.getList();
    this.getDeptTree();
  },
  methods: {
    handleStatusChange(row) {
      let text = row.effectStatus === "1" ? "启用" : "禁用";
      this.$modal
        .confirm('确认要"' + text + '""' + row.name + '"目录吗？')
        .then(function () {
          return effectgoodsDirectory(
            row.chemistryGoodsDirectoryId,
            row.effectStatus
          );
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.effectStatus = row.effectStatus === "0" ? "1" : "0";
        });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.chemistryGoodsDirectoryId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      const configId = row.chemistryGoodsDirectoryId || this.ids;
      this.$router.push("./addDirectory?isEdit=true&id=" + configId);
    },
    handleDetail(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      this.$router.push(
        "./addDirectory?isInfo=true&id=" + row.chemistryGoodsDirectoryId
      );
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const configId = row.chemistryGoodsDirectoryId || this.ids;
      let name = this.checkRow(
        configId,
        this.dataList,
        "chemistryGoodsDirectoryId",
        "DELETE",
        "name"
      );
      this.$modal
        .confirm('是否确认删除名称为"' + name + '"的数据项？')
        .then(function () {
          return delgoodsDirectory(configId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    handleAdd() {
      this.$router.push("./addDirectory");
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listgoodsDirectory({
        ...this.addDateRangeTime(
          this.queryParams,
          this.dateRange,
          "startEndDate",
          "endEndDate"
        ),
      }).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleExport() {
      this.queryParams.ids = this.ids.toString()
      this.download(
        "system/goodsDirectory/export",
        {
          ...this.queryParams,
        },
        `消耗品、标准物质名录表_${this.parseTime(
          new Date().getTime(),
          "{y}-{m}-{d}"
        )}.xlsx`
      );
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  img {
    width: 100%;
  }
}
</style>
